package com.yss.kettle.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yss.common.core.enums.YssStatusEnum;
import com.yss.common.core.utils.StringUtils;
import com.yss.common.core.web.domain.AjaxResult;
import com.yss.common.logger.annotation.Log;
import com.yss.common.logger.enums.BusinessType;
import com.yss.common.security.utils.DictUtils;
import com.yss.kettle.domain.*;
import com.yss.kettle.service.IJobDutiesService;
import com.yss.kettle.service.IJobDutiesTaskService;
import com.yss.kettle.service.IJobTaskDetailsService;
import com.yss.kettle.service.IJobTaskService;
import com.yss.system.api.domain.SysDictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yss.common.security.annotation.RequiresPermissions;
import com.yss.common.core.web.controller.BaseController;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 采集分类信息Controller
 *
 * @author yss
 * @date 2023-04-23
 */
@RestController
@RequestMapping("/category")
public class JobCategoryController extends BaseController {
    @Autowired
    private IJobTaskService jobTaskService;
    @Autowired
    private IJobDutiesService jobDutiesService;
    @Autowired
    private IJobDutiesTaskService jobDutiesTaskService;

    /**
     * 查询每个分类对应的作业任务信息
     */
    @RequiresPermissions("kettle:category:list")
    @GetMapping("/list/{dutiesId}")
    public AjaxResult list(@PathVariable("dutiesId") String dutiesId) {
        AjaxResult ajax = AjaxResult.success();
        List<SysDictData> list = DictUtils.getDictCache("kettle_category");
        Map<String, List<JobTask>> listMap = new LinkedHashMap<>();
        for (SysDictData dictData : list) {
            if (StringUtils.isNull(dictData) || StringUtils.isEmpty(dictData.getDictValue())) {
                continue;
            }
            QueryWrapper<JobTask> wrapper = new QueryWrapper<>();
            wrapper.eq("CATEGORY_TYPE", dictData.getDictValue());
            listMap.put(dictData.getDictLabel(), jobTaskService.list(wrapper));
        }
        ajax.put("categoryList", listMap); // 所有的作业任务
        //当前采集任务拥有的作业任务
        if (StringUtils.isNotEmpty(dutiesId)) {
            QueryWrapper<JobDutiesTask> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("DUTIES_ID", dutiesId);
            ajax.put("jobDutiesTaskList", jobDutiesTaskService.list(queryWrapper));
        }
        QueryWrapper<JobDuties> wrapper = new QueryWrapper<>();
        wrapper.eq("STATUS", YssStatusEnum.NORMAL.getCode());
        ajax.put("jobDuties", jobDutiesService.list(wrapper));
        return ajax;
    }

}
